package me.zhengjie.modules.website.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import me.zhengjie.modules.website.domain.Theme;
import me.zhengjie.modules.website.service.ThemeService;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import me.zhengjie.modules.website.domain.PageResult;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author system
 * @since 2024-10-13 09:38:00
 */
@RestController
@RequestMapping("/api/theme")
@Api(tags = " 网页-主题管理")
public class ThemeController {

    @Autowired
    private ThemeService themeService;

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @param startDate 起始时间
     * @param endDate   结束时间
     * @return 分页结果
     */
    @ApiOperation(value = "分页查询", notes = "分页查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/website/findPage")
    public ResponseEntity<PageResult<Theme>> findPage(
            @RequestBody Theme params,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endDate,
            Pageable pageable) {

        // 调用服务层方法执行分页查询
        PageResult<Theme> result = themeService.findPage(params, startDate, endDate, pageable);
        return ResponseEntity.ok(result);
    }


    /**
     * 列表查询
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "列表查询", notes = "列表查询", produces = "application/json")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @PostMapping("/website/findList")
    public ResponseEntity<List<Theme>> findList(@RequestBody Theme params) {
        List<Theme> result = themeService.findList(params);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功")})
    @GetMapping("/website/findById/{id}")
    public ResponseEntity<Theme> findById(@PathVariable("id") Long id) {
        Theme theme = themeService.findById(id);
        return ResponseEntity.ok(theme);
    }

    /**
     * 新增
     *
     * @param theme
     * @return
     */
    @ApiOperation(value = "新增", notes = "新增数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/insert")
    public ResponseEntity<Theme> insert(@Validated @RequestBody Theme theme) {
        Theme result = themeService.insert(theme);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改
     *
     * @param theme
     * @return
     */
    @ApiOperation(value = "修改", notes = "修改数据")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PutMapping("/update")
    public ResponseEntity<Theme> update(@Validated @RequestBody Theme theme) {
        Theme result = themeService.update(theme);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除", notes = "删除数据")
    @DeleteMapping("/del/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        themeService.delete(id);
        return ResponseEntity.ok(null);
    }


    /**
     * 切换主题启用状态
     *
     * @param newThemeId 新主题ID
     * @return ApiResponse
     */
    @PostMapping("/switch")
    @ApiOperation(value = "切换主题", notes = "切换启用的主题")
    public ResponseEntity<?> switchTheme(@RequestParam Long newThemeId) {
        try {
            // 切换主题
            themeService.switchTheme(newThemeId);

            // 成功时返回 HTTP 200 和成功消息
            return ResponseEntity.ok("主题切换成功");
        } catch (IllegalArgumentException e) {
            // 当业务逻辑出错时，比如主题ID不存在
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (RuntimeException e) {
            // 捕获所有其他运行时异常并返回 HTTP 500
            return ResponseEntity.status(500).body("服务器内部错误");
        }
    }


    /**
     *查询当前正在使用的主题
     *
     * @return
     */
    @ApiOperation(value = "展示平台-查询当前正在使用的主题", notes = "展示平台-查询当前正在使用的主题")
    @GetMapping("/website/enabled")
    public ResponseEntity<Theme> getEnabledTheme() {
        Theme theme = themeService.getEnabledTheme();
        if (theme != null) {
            return new ResponseEntity<>(theme, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND); // 如果没有找到记录，返回404
        }
    }
}